package com.Tadama.Forms;

import com.Tadama.MIDlet.TadamaMIDlet;
import com.Tadama.Utility.CommandBuilder;
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

public class frmNuevoCliente extends Form implements CommandListener{
    private Displayable previousDisplay;
    private TadamaMIDlet mainMIDlet;
    private Alert alerta;
    private TextField tfRazSocial;
    private TextField tfRuc;
    private TextField tfDireccion;
    private TextField tfTelefono;
    private TextField tfCelular;
    private TextField tfEmail;
    private TextField tfObservacion;
    private StringItem stringItem;
    private ChoiceGroup chgTipoCliente;
    
    private static final Command cmBack = CommandBuilder.getback();
    private static final Command cmEnviar = CommandBuilder.getEnviar();
    private static final Command cmOk = CommandBuilder.getOk();
    
    public frmNuevoCliente(TadamaMIDlet mainMIDlet,Displayable previousDisplay) {
        super("Nuevo Cliente");
        this.mainMIDlet= mainMIDlet;
        this.previousDisplay= previousDisplay;
        init();
    }
    
    private void init() {
        chgTipoCliente = new ChoiceGroup("Tipo Cliente", Choice.POPUP);
        chgTipoCliente.append("Juridico", null);
        chgTipoCliente.append("Natural",null);
        chgTipoCliente.setSelectedFlags(new boolean[]{false, false});
        tfRazSocial = new TextField("RazSocial", "Bodega Pepido", 50, TextField.ANY);
        tfRuc = new TextField("Ruc", "70181463130", 20, TextField.ANY);
        tfDireccion = new TextField("Direccion", "Alto Trujillo #123", 20, TextField.ANY);
        tfTelefono = new TextField("Telefono", "044891534", 10, TextField.ANY);
        tfCelular = new TextField("Celular", "971897125", 20, TextField.ANY);
        tfEmail = new TextField("Correo", "", 20, TextField.ANY);
        tfObservacion = new TextField("Observacion", "bodega pequeña", 200, TextField.ANY);
        this.append(chgTipoCliente);
        this.append(tfRazSocial);
        this.append(tfRuc);
        this.append(tfDireccion);
        this.append(tfTelefono);
        this.append(tfCelular);
        this.append(tfEmail);
        this.append(tfObservacion);
        
        this.addCommand(cmBack);
        this.addCommand(cmEnviar);
        this.setCommandListener(this);
    }
    
    public Alert getAlert() {
        if (alerta == null) {
            alerta = new Alert("Confirmacion", "Cliente se Registro Satisfactriamente", null, AlertType.CONFIRMATION);
            alerta.addCommand(cmOk);
            alerta.setCommandListener(this);
            alerta.setTimeout(Alert.FOREVER);
        }
        return alerta;
    }
    
    private static Image loadImage(String name) {
        Image image = null;
        try {
            image = Image.createImage(name);
        }catch (IOException ioe){System.out.println(ioe);}
        return image;
    }

    public void commandAction(Command c, Displayable d) {
        if (d==this){
            if (c == cmBack) {mainMIDlet.destroyApp(true);}
            if(c==cmEnviar)
                Display.getDisplay(mainMIDlet).setCurrent(getAlert(), this);
        }
        if (d==alerta)
        {
            if(c == cmOk)
                Display.getDisplay(mainMIDlet).setCurrent(new lstMenu(mainMIDlet,"Menu Principal",List.IMPLICIT));
        }
    }
}